package com.auto.learning.net;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

import org.apache.http.HttpEntity;
import org.apache.http.client.ClientProtocolException;

import android.content.Context;
import android.os.Message;

import com.auto.learning.event.Handle;

public class DownLoadHelper {

	/**
	 * 取得待下载文件的大小
	 * 
	 * @param context
	 * @param url
	 * @return
	 * @throws ClientProtocolException
	 * @throws IOException
	 */
	public static long getFileLength(Context context, String url)
			throws ClientProtocolException, IOException {
		HttpEntity entity = HttpHelper.getInfoByGet(url, null, context);
		if (entity == null) {
			return 0;
		}
		return entity.getContentLength();
	}

	/**
	 * 取得文件的inputStream
	 * 
	 * @param context
	 * @param url
	 * @return
	 * @throws ClientProtocolException
	 * @throws IOException
	 */
	public static InputStream getFileInputStream(Context context, String url,
			long fileLength) throws ClientProtocolException, IOException {
		HttpEntity entity = HttpHelper.getInfoByGetByHeader(url, null, context, fileLength);
		if (entity == null) {
			return null;
		}
		return entity.getContent();
	}

	/**
	 * 取得文件的inputStream
	 * 
	 * @param context
	 * @param url
	 * @return
	 * @throws ClientProtocolException
	 * @throws IOException
	 */
	public static InputStream getFileInputStream(Context context, String url) throws ClientProtocolException, IOException {
		HttpEntity entity = HttpHelper.getInfoByGet(url, null, context);
		if (entity == null) {
			return null;
		}
		return entity.getContent();
	}

	/**
	 * 判断文件是否�?要下�?
	 * 
	 * @param context
	 * @param filePath
	 * @param downLoadUrl
	 * @return 返回�?要下载文件的SIZE
	 */
	public static long isNeedDownFile(Context context, String filePath,
			String downLoadUrl, long fileSize) {
		/*
		String downLoadFileName = CommonMethod.getFileName(downLoadUrl);
		return CommonMethod.compareMainPlayFile(downLoadFileName, filePath, fileSize);
		 */
		return 0;
	}
	
	/**
	 * 判断文件是否�?要下�?
	 * 
	 * @param context
	 * @param filePath
	 * @param downLoadUrl
	 * @return 返回�?要下载文件的SIZE
	 */
	/*public static long isNeedDownFile(Context context,DownLoadMusicBean bean) {
		return CommonMethod.compareMainPlayDB(context, bean);
	}*/

	/**
	 * @param in
	 * @param out
	 * @param handler
	 * @throws IOException
	 */
	public static void readData(InputStream in, OutputStream out,
			Handle handler, long fileLength, String fileName)
			throws IOException {
		int BUFFER_SIZE = 16 * 1024;
		in = new BufferedInputStream(in, BUFFER_SIZE);
		out = new BufferedOutputStream(out, BUFFER_SIZE);
		byte[] buffer = new byte[BUFFER_SIZE];
		int len;
		int noticeLength = 50000;
		int loadedLength = 0;// 已下载的长度（kb�?
		while ((len = in.read(buffer, 0, BUFFER_SIZE)) > 0) {
			out.write(buffer, 0, len);
			loadedLength += len;// 每下�?1kb，该�?+1
			if (loadedLength > noticeLength && handler != null) {
				noticeLength += loadedLength;
				Message msg = handler.obtainMessage(2);
				msg.arg1 = loadedLength;
				msg.arg2 = (int) fileLength;
				msg.obj = fileName;
				if (fileLength > 0 && loadedLength < fileLength) {
					handler.sendMessage(msg);
				}
			}
		}
		Message msg = handler.obtainMessage(2);
		msg.arg1 = (int) fileLength;
		msg.arg2 = (int) fileLength;
		msg.obj = fileName;
		handler.sendMessage(msg);
		out.flush();
		if (null != in) {
			in.close();
		}
		if (null != out) {
			out.close();
		}
	}
	
	/*
	 	int BUFFER_SIZE = 16 * 1024;
		in = new BufferedInputStream(in, BUFFER_SIZE);
		out = new BufferedOutputStream(out, BUFFER_SIZE);
		byte[] buffer = new byte[BUFFER_SIZE];
		int len;
		int noticeLength = 50000;
		int loadedLength = 0;// 已下载的长度（kb�?
		while ((len = in.read(buffer)) > 0) {
			out.write(buffer, 0, len);
			loadedLength += len;// 每下�?1kb，该�?+1
			if (handler != null) {
				noticeLength += loadedLength;
				Message msg = handler.obtainMessage(ActivityInfoHelper.SERVICE_HANDLER_UPDATE_PROGRESS);
				msg.arg1 = loadedLength;
				msg.arg2 = (int) fileLength;
				msg.obj = fileName;
				if (fileLength > 0 && loadedLength < fileLength) {
					handler.sendMessage(msg);
				}
			}
		}
		Message msg = handler.obtainMessage(ActivityInfoHelper.SERVICE_HANDLER_UPDATE_PROGRESS);
		msg.arg1 = (int) fileLength;
		msg.arg2 = (int) fileLength;
		msg.obj = fileName;
		handler.sendMessage(msg);
		out.flush();
		if (null != in) {
			in.close();
		}
		if (null != out) {
			out.close();
		}
	 */

	/**
	 * 把文件存入SD�?
	 * 
	 * @param in
	 * @param path
	 * @param handler
	 * @param fileLength
	 * @throws IOException
	 */
	public static void saveCard(InputStream in, String path,
			boolean isStartFile, String fileName, Handle handler,
			long fileLength) throws IOException {
		File file = new File(path);
		if (!file.exists()) {
			file.mkdir();
		}
		FileOutputStream out = null;
		if (isStartFile) {
			out = new FileOutputStream(path + "/" + fileName);
		} else {
			out = new FileOutputStream(path + "/" + fileName, true);
		}
		readData(in, out, handler, fileLength, fileName);
	}

}
